package com.graduation.check.controller;

import com.graduation.check.dto.SubmissionListRequest;
import com.graduation.check.dto.SubmissionListResponse;
import com.graduation.check.service.TeacherSubmissionService;
import com.graduation.check.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher/submissions")  // context-path=/api已在application.properties配置
@CrossOrigin(origins = "*")
public class TeacherSubmissionController {
    
    @Autowired
    private TeacherSubmissionService teacherSubmissionService;
    
    /**
     * 获取提交记录列表
     */
    @GetMapping
    public ApiResponse<SubmissionListResponse> getSubmissionList(SubmissionListRequest request) {
        try {
            SubmissionListResponse response = teacherSubmissionService.getSubmissionList(request);
            return ApiResponse.success(response);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取提交列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取提交详情
     */
    @GetMapping("/{submissionId}")
    public ApiResponse<Map<String, Object>> getSubmissionDetail(@PathVariable Long submissionId) {
        try {
            Map<String, Object> detail = teacherSubmissionService.getSubmissionDetail(submissionId);
            return ApiResponse.success(detail);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取提交详情失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量下载报告
     */
    @PostMapping("/download-reports")
    public ApiResponse<Map<String, Object>> batchDownloadReports(
            @RequestBody Map<String, Object> request) {
        @SuppressWarnings("unchecked")
        List<Long> submissionIds = (List<Long>) request.get("submissionIds");
        String downloadType = (String) request.getOrDefault("downloadType", "reports");
        
        String downloadUrl = teacherSubmissionService.batchDownloadReports(submissionIds, downloadType);
        
        Map<String, Object> result = Map.of(
            "downloadUrl", downloadUrl,
            "fileName", "reports_batch.zip",
            "fileCount", submissionIds.size()
        );
        
        return ApiResponse.success("打包完成", result);
    }
}
